home *** CD-ROM | disk | FTP | other *** search
- ' this class is a sample HTTP handler that accepts requests for a
- ' resource named table.xyz and displays the contents of the Biblio database
- ' table with that name. You can optionally pass a WHERE clause on the query
- ' string, as follows
- ' publishers.xyz?city='New York'
-
- ' it is enabled by the following lines in web.config
- '<httpHandlers>
- ' <add verb="*" path="*.xyz" type="AspnetApplications.XyzHandler,AspnetApplications" />
- '</httpHandlers>
-
- Imports System.Data.OleDb
-
- Public Class XyzHandler
- Implements IHttpHandler
-
- ' this method is called whenever an .xyz request is posted to the server
-
- Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
- ' The name of the page is the table table.
- Dim tableName As String = context.Request.Path
- ' drop directory name, if any, and the extension.
- tableName = System.IO.Path.GetFileNameWithoutExtension(tableName)
- ' the query string is an optional WHERE clause
- Dim whereClause As String = context.Request.QueryString.ToString
-
- ' Build the SQL query.
- Dim sql As String = "SELECT * FROM " & tableName
- If whereClause.Length > 0 Then sql &= " WHERE " & whereClause
-
- ' Send the table to the client to the client.
- context.Response.Write("<HTML><BODY>")
- context.Response.Write(MakeHtmlTable(sql, context))
- context.Response.Write("</BODY></HTML>")
- End Sub
-
- ' this method is part of the IHttpHandler interface
-
- Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
- Get
- Return True
- End Get
- End Property
-
- ' Perform an SQL query, return the result as an HTML table.
-
- Function MakeHtmlTable(ByVal sql As String, ByVal context As HttpContext) As String
- Dim cn As New System.Data.OleDb.OleDbConnection(BiblioConnString)
- Dim cmd As New System.Data.OleDb.OleDbCommand(sql, cn)
- Dim dr As System.Data.OleDb.OleDbDataReader
-
- ' Use a StringBuilder to create the output.
- Dim sb As New System.Text.StringBuilder(10240)
-
- Try
- ' Open a connection to Biblio and process the query.
- cn.Open()
- dr = cmd.ExecuteReader
-
- ' Create an HTML table with correct header row.
- sb.Append("<TABLE Border='1'><THEAD>")
- Dim i As Integer
- For i = 0 To dr.FieldCount - 1
- sb.Append("<TH>" & dr.GetName(i) & "</TH>")
- Next
- sb.Append("</THEAD>")
-
- ' Output data for each record.
- Do While dr.Read
- sb.Append("<TR>")
- For i = 0 To dr.FieldCount - 1
- ' A single field value (must be encoded for html).
- sb.Append("<TD>")
- If Not dr.IsDBNull(i) Then
- sb.Append(context.Server.HtmlEncode(dr(i).ToString))
- Else
- sb.Append("(null)")
- End If
- sb.Append("</TD>")
- Next
- sb.Append("</TR>")
- Loop
- ' close the table .
- sb.Append("</TABLE>")
-
- Catch ex As Exception
- sb.Append("<h1>Unable to process the request</h1>")
- Finally
- ' Close the data reader and the connection, if necessary.
- If Not (dr Is Nothing) AndAlso Not dr.IsClosed Then dr.Close()
- If cn.State = ConnectionState.Open Then cn.Close()
- End Try
-
- ' Return the HTML text to the caller.
- Return sb.ToString
- End Function
-
- End Class
-